/*
 * seqFeederFile.cpp
 *
 *  Created on: 2011-09-04
 *      Author: pawel
 */
#include"seqFeederFile.h"
#include"PerlComp/ProteicAlphabetPerl.h"
#include <Bpp/Seq/Io/Fasta.h>
#include<Bpp/Seq/Sequence.h>
#include<Bpp/Seq/Alphabet/DNA.h>
#include<Bpp/Seq/Alphabet/RNA.h>
#include<exception>
#include<iostream>
seqFeederFile::seqFeederFile(const std::string& filename,AlphabetType alpt)
:seqFeeder(),alphabetPtr(NULL)
{
	//TODO ERRORS even if file exists check it

	try{
		inFile.exceptions(std::ifstream::badbit);
		inFile.open(filename.c_str(),std::ifstream::in);
		closed = inFile.eof();
		productionEnded =empty = closed;
		//TODO Find IO EXceptions class
		if(inFile.fail()){
			closed = true;
			throw std::ifstream::failure("Can not open: "+filename);
		}
	}catch(std::ifstream::failure& e){
		throw;
	}

	switch(alpt){
	case AA:{
		std::auto_ptr<bpp::Alphabet> smartAlphabetAA(new ProteicAlphabetPerl);
		alphabetPtr = smartAlphabetAA.release();
	}
		break;
		//TODO Implement own Alphabet Class Compatibile with perl Alphabet
	case DNA:{
		std::auto_ptr<bpp::Alphabet> smartAlphabetDNA(new bpp::DNA);
		alphabetPtr = smartAlphabetDNA.release();
	}
		break;
	case RNA:{
		std::auto_ptr<bpp::Alphabet> smartAlphabetRNA(new bpp::RNA);
		alphabetPtr = smartAlphabetRNA.release();
	}
		break;
	default:
			throw std::logic_error("Unknown alphabet type!\n");
		break;
	};

}
seqFeederFile::~seqFeederFile(){
	if(alphabetPtr != NULL){
			delete alphabetPtr;
			alphabetPtr = NULL;
	}
	inFile.close();
}
bpp::BasicSequence* seqFeederFile::getSeq(){

	bpp::BasicSequence* retPtr;
	retPtr = new bpp::BasicSequence(alphabetPtr);
	bpp::Fasta fastaSeq;
	if(!inFile.eof()){
		fastaSeq.nextSequence(inFile,*retPtr);
	}
	closed = inFile.eof();
	empty = closed;

	return retPtr;

}
void seqFeederFile::pushSeq(bpp::BasicSequence*){}

void seqFeederFile::pushAddParams(seqAlignParams *params, std::string seqName)
{
}



seqAlignParams *seqFeederFile::getAddParam(std::string seqName)
{
	return NULL;
}


